package org.iot.iotserver.http.servlet;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet(name = "YIOTServlet", urlPatterns = { "/jsonp/*" })
public class YCrossOriginServlet extends YAbstractServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, Object> obj = new LinkedHashMap<>();
        obj.put("code", 1);
        obj.put("message","success");
        obj.put("params",req.getParameterMap());
        responseJsonData(
                resp,
                String.format(
                        "%s(%s);",
                        StrUtil.blankToDefault(req.getParameter("callback"), "callback"),
                        JSONUtil.toJsonStr(obj)
                )
        );
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
